'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:PersonnelCcsqCtrl
 * @description
 * # PersonnelCcsqCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('PersonnelCcsqCtrl', function ($scope, $rootScope, openApi, $http, Util, $location, $routeParams, FormSubmit) {
    $scope.sheetObj = {};
    // 底部关联模块类型选框添加
    var relModuleObj = {},
      selectData, params;
    relModuleObj.sheettitle = "关联模块";
    relModuleObj.callback = function (item) {
      if (item.selectTabContent == "无") {
        $scope.param.relatedModuleName = "无";
        $scope.param.relatedModule = "";
        $scope.param.moduleNo = "";
        return;
      }
      $scope.param.relatedModule = item.selectTab;
      $scope.param.relatedModuleName = item.selectTabContent;
      $scope.param.moduleNo = ""

    }
    $scope.sheetObj.relModuleObj = relModuleObj;
    $scope.showrelModule = function () {
      Util.getModuleRel(Util.CC_MODULE, function (data) {
        $scope.sheetObj.relModuleObj.tabList = data.data
        $scope.showSheet("relModuleObj")
      })

    };
    $scope.showSheet = function (type) {
      var sheetObj = $scope.sheetObj[type]
      $scope.sheetObj.type = type
      $scope.sheetObj.tabList = sheetObj.tabList
      $scope.sheetObj.sheettitle = sheetObj.sheettitle
      $scope.sheetObj.ifshowSheet = true
      $rootScope.showMasker = true
    };
    $scope.chooseinfo = function () {
      // $routeParams.id通过id确定唯一回显id
      if (!$scope.param.relatedModule) {
        return
      }
      Util.savaParamForReview($scope.param, $routeParams.id);
      // 里面包含了title
      var relModuleObj = {
        name: "PersonnelCcsqCtrl",
        selectTab: $scope.param.relatedModule,
        selectTabContent: $scope.param.relatedModuleName
      };
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosepeople")

    };
    //获取出场人员列表
    $scope.getOutUsers = function () {
      Util.saveSelectLists($scope.param.attendanceDetailsArr);
      Util.savaParamForReview($scope.param, $routeParams.id);
      $location.path("administrator/chooseinfo/ccsq");
    };
    // 删除出场人员列表
    $scope.deletedItem = function (index) {
      $scope.param.attendanceDetailsArr.splice(index, 1);
      Util.saveSelectLists($scope.param.attendanceDetailsArr);
    };
    // 获取选择的人员
    var getAllUsers = function () {
      var users = [];
      users = $scope.param.attendanceDetailsArr.map(function (item, index) {
        return {
          "ownerName": item.ownerName,
          "certificateType": item.certificateType,
          "attendanceCost": item.attendanceCost,
          "cerId": item.cerId,
          "remark": item.remark
        }
      });
      return JSON.stringify(users);
    };
    var checkUsers = function () {
      var check = true;
      $scope.param.attendanceDetailsArr.forEach(function (item, index) {
        if (!item.attendanceCost) {
          check = false;
          return false
        }
      })

      return check;
    }
    // 提交申请
    $scope.submit = function () {

      if (!$scope.param.attendanceDate) {
        Util.Toast("请选择出场日期");
        return;
      }
      if (!$scope.param.address) {
        Util.Toast("请选择出场地点");
        return;
      }
      if ($scope.param.relatedModuleName && $scope.param.relatedModuleName !== "无") {
        if (!$scope.param.moduleNo) {
          Util.Toast("请选择模块编号");
          return;
        }
      }
      if ($scope.param.attendanceDetailsArr.length < 1) {
        Util.Toast("请添加出场人员");
        return;
      }
      var check = checkUsers();
      if (!check) {
        Util.Toast("请填写出场费用")
        return;
      }
      $scope.param.attendanceDetails = getAllUsers();
      // 提交
      var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.attendanceApply;
      var options = {};
      options.urlParam = urlParam;
      var myDest = {};
      options.param = angular.copy($scope.param, myDest);
      var TVtype;
      TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
      // 如果是修改页面的话则提交的地址不一样，参数一样
      delete options.param.attendanceDetailsArr
      if (TVtype == Util.UPDATE) {
        options.url = openApi.ip + openApi.attendanceApply
      }
      new FormSubmit(options);
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData($routeParams.id, function (data) {
        $scope.param = data;
        // 获取选择页面返回的数据并且合并到参数中
        // 用来显示选择的报销明细
        var selectLists;
        selectLists = Util.getSelectLists();
        $scope.param.attendanceDetailsArr = selectLists || $scope.param.details || [];
      });
      // 是否显示审批记录
      Util.showloglist($routeParams.id, function (data) {
        $scope.logData = data;
      });
    };
    // 初始化
    init();
  });
